package com.knife.hospital.service.department;

import com.knife.hospital.config.BaseException;
import com.knife.hospital.constant.ResponseCode;
import com.knife.hospital.entity.DepartmentPO;
import com.knife.hospital.entity.DoctorPO;
import com.knife.hospital.mapper.DepartmentPOMapper;
import com.knife.hospital.mapper.DoctorPOMapper;
import com.knife.hospital.model.department.DepartmentInfoResponse;
import com.knife.hospital.model.department.IdCard;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService{
    @Resource
    private DepartmentPOMapper departmentPOMapper;
    @Resource
    private DoctorPOMapper doctorPOMapper;

    @Override
    public List<IdCard> getDepartmentList() {
        List<DepartmentPO> departmentPOs = departmentPOMapper.getAllDepartment();
        List<IdCard> idCards = new LinkedList<>();
        for (DepartmentPO departmentPO : departmentPOs) {
            idCards.add(new IdCard(departmentPO.getId(), departmentPO.getName()));
        }
        return idCards;
    }

    @Override
    public DepartmentInfoResponse getDepartmentInfo(int id) {
        DepartmentInfoResponse departmentInfoResponse = new DepartmentInfoResponse();
        List<IdCard> idCards = new LinkedList<>();

        DepartmentPO departmentPO= departmentPOMapper.selectByPrimaryKey(id);

        //不存在的科室返回异常
        if(departmentPO == null) throw new BaseException(ResponseCode.RESOURCES_NOT_EXIST);

        //获取医生列表并组装idCards
        List<DoctorPO> doctorPOs = doctorPOMapper.selectByDepartment(id);
        for(DoctorPO doctorPO : doctorPOs){
            idCards.add(new IdCard(doctorPO.getId(),doctorPO.getName()));
        }

        departmentInfoResponse.setDepartmentInfo(departmentPO);
        departmentInfoResponse.setDoctorList(idCards);
        return departmentInfoResponse;
    }
}
